package com.andyliu.system.service.impl;

import com.andyliu.system.dto.DeptDto;
import com.andyliu.system.entity.Dept;
import com.andyliu.system.entity.Ztree;
import com.andyliu.system.mapper.DeptMapper;
import com.andyliu.system.service.IDeptService;
import com.andyliu.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author andyLiu
 * @since 2021-08-10
 */
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements IDeptService {
    @Autowired
    private DeptMapper deptMapper;

      public  IPage<Dept> selectByPage(Integer pageNumber,Integer pageSize, Dept dept){
        return deptMapper
                .selectByPage(new Page<Dept>(pageNumber,pageSize),dept);
      }

    @Override
    public List<Dept> selectDepts(Dept dept) {
        QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
        if(dept!=null){
            if(!StringUtil.isEmpty(dept.getDeptName())){
                queryWrapper.like("dept_name",dept.getDeptName());
            }
        }
        return deptMapper.selectList(queryWrapper);
    }

    @Override
    public List<Ztree> queryDeptForTree(Dept deptment) {
        List<Dept> depts = selectDepts(deptment);
        List<Ztree> data = new ArrayList<>();
          for(Dept dept:depts){
              Ztree ztree = new Ztree();
              ztree.setId(dept.getDeptId());
              ztree.setName(dept.getDeptName());
              ztree.setTitle(dept.getDeptName());
              ztree.setChecked(false);
              ztree.setOpen(false);
              ztree.setpId(dept.getParentId());
              data.add(ztree);
          }
        return data;
    }

    @Override
    public DeptDto queryDeptById(Integer id) {
        return deptMapper.queryDeptById(id);
    }
}
